/** @type {import('next').NextConfig} */

const nextConfig = {
	webpack(config, { isServer, dev }) {
		// Use the client static directory in the server bundle and prod mode
		// Fixes `Error occurred prerendering page "/"`
		config.output.webassemblyModuleFilename =
			isServer && !dev
				? "../static/wasm/[modulehash].wasm"
				: "static/wasm/[modulehash].wasm";

		// Since Webpack 5 doesn't enable WebAssembly by default, we should do it manually
		config.experiments = { ...config.experiments, asyncWebAssembly: true };
		// Module not found: Can't resolve 'fs'
		config.resolve.fallback = {
			...config.resolve.fallback, // if you miss it, all the other options in fallback, specified
			// by next.js will be dropped. Doesn't make much sense, but how it is
			fs: false, // the solution
		};

		config.module.rules.push({
			test: /\.wasm/,
			type: 'asset/resource'
		})
		return config;
	}
}

const withPWA = require('next-pwa')({
	dest: 'public',
	register: true,
	skipWaiting: true,
	disable: process.env.NODE_ENV === 'development',
	sw: '/service-worker.js',
	runtimeCaching: [
		{
			urlPattern: /[.](png|jpg|ico|css)/,
			handler: 'CacheFirst',
			options: {
				cacheName: 'assets-cache',
				cacheableResponse: {
					statuses: [0, 200],
				},
			},
		},
		{
			urlPattern: /^http.*/,
			handler: 'NetworkFirst',
			options: {
				cacheName: 'http-cache',
			},
		},
	],
})

module.exports = withPWA(nextConfig)
